package misty.mispic.app.ui.main

import groovy.transform.CompileStatic
import javafx.beans.value.ChangeListener
import javafx.fxml.FXML
import javafx.fxml.Initializable
import javafx.scene.control.Control
import javafx.scene.control.Skin
import javafx.scene.control.TreeItem
import javafx.scene.control.TreeView
import javafx.scene.layout.StackPane
import misty.mispic.app.comp.FileTreeCell
import misty.mispic.app.comp.FileTreeItem
import misty.mispic.app.event.EventBus
import misty.mispic.app.event.ThumbEvent
import misty.mispic.app.ui.photo.Photo
import misty.mispic.app.ui.thumb.Thumb2

import java.nio.file.FileSystems
import java.nio.file.Path

/**
 * Created by Misty on 2015/1/15.
 */
@CompileStatic
class Main extends Control implements Initializable {
    @FXML
    private TreeView mFileTree;

    @FXML
    private StackPane mContent;

    @FXML
    private StackPane mRoot;

    @Override
    protected Skin createDefaultSkin() {
        return new MainSkin(this)
    }

    @Override
    void initialize(URL location, ResourceBundle resources) {
        setupFileTree()
        setupThumbView()
        setupPhotoView()
    }

    private void setupFileTree() {
        mFileTree.cellFactory = FileTreeCell.factory
        mFileTree.root = new FileTreeItem(FileSystems.default)

        mFileTree.selectionModel.with {
            clearSelection()
            selectedItemProperty().addListener({ obs, ov, TreeItem nv ->
                if (nv?.value) {
                    EventBus.post(new ThumbEvent.Show(nv.value as Path))
                }
            } as ChangeListener)
        }
    }

    private void setupThumbView() {
        mContent.children << new Thumb2()
    }

    private void setupPhotoView() {
        def photo = new Photo()
        mRoot.children << photo
        photo.toFront()
    }
}
